
module.exports.parse = function(input, flag) {
  if (input === null || input === '') {
    return {};
  }
  if ((typeof input) !== 'string') {
    throw new TypeError('invalidate arguments[0]');
  }
  const reg = new RegExp('[\?\&][^\?\&]+=[^\?\&]+', 'g');
  let result = input.match(reg);
  const query = {};
  if (result) {
    for (var i = 0; i < result.length; i++) {
      var temp = result[i].substring(1);
      var arr = temp.split('=');
      var value = arr[1];
      if (flag === true) {
        value = decodeURIComponent(value);
      }
      query[arr[0]] = value;
    }
  }
  return query;
};

module.exports.stringify = function(query, flag) {
   if (query === null || query === undefined) {
     return '';
   }
   if ((typeof query) !== 'object') {
     throw new TypeError('invalidate arguments[0]');
   }
   let search = '';
   for (var key in query) {
     if (query.hasOwnProperty(key)) {
       search +=  '&' + key + '=' + (query[key] ? encodeURIComponent(query[key]) : '');
     }
   }
   if (flag) {
     search = search.substring(1);
   }
   return search;
};
